﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasicDataManagement;
using DataModel;

namespace CheckStockManagement
{
    public partial class MainForm : Form
    {
        OpFromDB op;
        List<CheckStock> matStockList;
        List<string> fixedDishCat;
        List<string> optionCat;
        int[] ForecastID;
        int IDsum = 0;
        int IDsum1 = 0;
        int[] ForecastID1;

        int[] Nums;
        int[] Nums1;

        public MainForm()
        {
            InitializeComponent();
            op=OpFromDB.GetInstance();
            CheckProcomboBox.Text = "日盘点";
            ViewDiffcomboBox.Text = "日盘点";
            TimecomboBox.Text = "近一周";
        }

        private void ViewCheckbutton_Click(object sender, EventArgs e)
        {

            ViewStocklistView.Items.Clear();
            int type=0;
            if (CheckProcomboBox.Text == "日盘点") {
                type = 1;
            }
            else if (CheckProcomboBox.Text == "周盘点")
            {
                type = 2;
            }
            else if (CheckProcomboBox.Text == "月盘点")
            {
                type = 3;
            }

            List<MaterialObject> mats = op.LoadMaterialObjects();
            matStockList = new List<CheckStock>();
            foreach (var t in mats)
            {
                if (t.CheckProperty == type || type == 0)
                {
                    //获取今天的物品支持总额
                    List<StockRecordItem> tmpSRD = op.LoadMaterialStockRecordsMaterial(t.ObjectName,DateTime.Now.Date);
                    //计算每个原料的现存量
                    double leftV = 0.0;
                    foreach (var s in tmpSRD)
                    {
                        if (s.StockDir == StockDirection.PutIn)
                        {
                            leftV += s.Value;
                            continue;
                        }
                        else
                        {
                            leftV -= s.Value;
                        }
                    }
                    CheckStock cs = new CheckStock();
                    cs.Date = DateTime.Now;
                    cs.CheckType = t.CheckProperty;
                    cs.MaterialName = t.ObjectName;
                    cs.OldStock = t.Stock+(float)leftV;
                    cs.Unit = t.Unit;
                    cs.Difference = cs.OldStock - cs.TodayStock;
                    cs.MaterialID = t.ID;
                    string checkType = null;
                    if (cs.CheckType == 1)
                    {
                        checkType = "日盘点";
                    }
                    else if (cs.CheckType == 2)
                    {
                        checkType = "周盘点";
                    }
                    else if (cs.CheckType == 3)
                    {
                        checkType = "月盘点";
                    }
                    matStockList.Add(cs);
                    ListViewItem lit = new ListViewItem(new string[] { cs.MaterialName, cs.Unit, cs.OldStock.ToString(), cs.TodayStock.ToString(), cs.Difference.ToString(), checkType });
                    ViewStocklistView.Items.Add(lit);
                }
            }

            
        }

        private void ViewStocklistView_DoubleClick(object sender, EventArgs e)
        {
            int slvi = ViewStocklistView.SelectedIndices[0];
            ListViewItem lvi = ViewStocklistView.SelectedItems[0];
            TextBox tb = new TextBox();
            tb.LostFocus += new EventHandler(this.Text_Blur);
            tb.Width = ViewStocklistView.Items[slvi].SubItems[3].Bounds.Width;
            tb.Location = new Point(ViewStocklistView.Items[slvi].SubItems[3].Bounds.Left, lvi.Bounds.Top);
            ViewStocklistView.Controls.Add(tb);
            tb.Focus();
            tb.Text = ViewStocklistView.Items[slvi].SubItems[3].Text;
        }

        private void Text_Blur(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            ListViewItem lvi = ViewStocklistView.SelectedItems[0];
            int slvi = ViewStocklistView.SelectedIndices[0];
            ViewStocklistView.Items[slvi].SubItems[3].Text = tb.Text;
            tb.Parent.Controls.Remove(tb);
            ViewStocklistView.Items[slvi].SubItems[4].Text = (Convert.ToDouble(ViewStocklistView.Items[slvi].SubItems[2].Text) - Convert.ToDouble(ViewStocklistView.Items[slvi].SubItems[3].Text)).ToString();
        }
        //生成差量表
        private void GenDifferencebutton_Click(object sender, EventArgs e)
        {
            int sum = ViewStocklistView.Items.Count;
            int t = 0;
            DateTime now=DateTime.Now;
            for (int i = 0; i < sum; i++) {
                
               ListViewItem.ListViewSubItemCollection subItme=ViewStocklistView.Items[i].SubItems;
               if (subItme[5].Text == "日盘点")
               {
                   t = 1;
               }
               else if (subItme[5].Text == "周盘点")
               {
                   t = 2;
               }
               else if (subItme[5].Text == "月盘点")
               {
                   t = 3;
               }
               op.AddCheckStockRecord(now, subItme[0].Text, subItme[1].Text, (float)Convert.ToDouble(subItme[2].Text), (float)Convert.ToDouble(subItme[3].Text), (float)Convert.ToDouble(subItme[4].Text), t, matStockList[i].MaterialID);
            }

            MessageBox.Show("生成完毕！");
        }
        //查看今日差量表
        private void ViewDiffbutton_Click(object sender, EventArgs e)
        {
            ViewDifflistView.Items.Clear();
            int t = 0;

            if (ViewDiffcomboBox.Text == "日盘点")
            {
                t = 1;
            }
            else if (ViewDiffcomboBox.Text == "周盘点")
            {
                t = 2;
            }
            else if (ViewDiffcomboBox.Text == "月盘点")
            {
                t = 3;
            }
            List<CheckStock> CSList = op.LoadCheckStockRecord(ViewDiffdateTimePicker.Value.Date, t);
            if (CSList.Count == 0) {
                MessageBox.Show("当日无该盘点属性的记录！");
            }
            foreach (CheckStock cs in CSList) {
                string checkType = null;
                if (cs.CheckType == 1)
                {
                    checkType = "日盘点";
                }
                else if (cs.CheckType == 2)
                {
                    checkType = "周盘点";
                }
                else if (cs.CheckType == 3)
                {
                    checkType = "月盘点";
                }

                ListViewItem lis = new ListViewItem(new string[] { cs.MaterialName, cs.Unit, cs.OldStock.ToString(), cs.TodayStock.ToString(), cs.Difference.ToString(), checkType });
                ViewDifflistView.Items.Add(lis);

            }
        }
        //生成采购表
        private void ViewPurbutton_Click(object sender, EventArgs e)
        {
            ViewPurlistView.Items.Clear();

            

            fixedDishCat = new List<string>(op.LoadFixedDishCategory());
            foreach (string s in fixedDishCat)
            {
                List<FixedDishObject> fdObjList = new List<FixedDishObject>(op.LoadFixedDishObjects(s));
                IDsum += fdObjList.Count;
            }
            
            optionCat = new List<string>(op.LoadOptionCategory());
            foreach (string s in optionCat)
            {
                List<OptionObject> opObjList = new List<OptionObject>(op.LoadOptionObjects(s));
                IDsum1 += opObjList.Count;
            }

            ForecastID = new int[IDsum];
            ForecastID1 = new int[IDsum1];
            Nums = new int[IDsum];
            Nums1 = new int[IDsum1];
            IDsum = 0;
            IDsum1 = 0;

            //if (ClasscomboBox.Text == "单品") {
            DateTime date = new DateTime();
            DateTime today = DateTime.Now;
            if (TimecomboBox.Text == "昨天") {
                date = today.AddDays(-1);
            }
            else if (TimecomboBox.Text == "近一周") {
                date = today.AddDays(-7);
            }
            else if (TimecomboBox.Text == "近1个月")
            {
                date = today.AddMonths(-1);
            }
            else if (TimecomboBox.Text == "近3个月")
            {
                date = today.AddMonths(-3);
            }
            else if (TimecomboBox.Text == "近半年")
            {
                date = today.AddMonths(-6);
            }
            else if (TimecomboBox.Text == "近一年")
            {
                date = today.AddYears(-1);
            }
            int days = (today - date).Days;

            List<ForecastInfo> FDforecastList = op.LoadFixedDishCount(date.Date);

                fixedDishCat = new List<string>(op.LoadFixedDishCategory());
                foreach (string s in fixedDishCat)
                {
                    List<FixedDishObject> fdObjList = new List<FixedDishObject>(op.LoadFixedDishObjects(s));

                    foreach (FixedDishObject f in fdObjList)
                    {
                        float count=0;
                        foreach (ForecastInfo i in FDforecastList)
                        {
                            if (i.ID == f.ID) count = i.Count/(float)days;
                        }
                        ViewPurlistView.Items.Add(new ListViewItem(new string[] {"单品",s,f.ObjectName,count.ToString(),"0" }));
                        ForecastID[IDsum] = f.ID;
                        IDsum++;
                    }
                }
           // }
           // else if (ClasscomboBox.Text == "配菜")
            //{
                List<ForecastInfo> OPforecastList = op.LoadOptionCount(date.Date);

                optionCat = new List<string>(op.LoadOptionCategory());
                foreach (string s in optionCat) {
                    List<OptionObject> opObjList = new List<OptionObject>(op.LoadOptionObjects(s));
                    foreach (OptionObject o in opObjList) {
                        int count = 0;
                        foreach (ForecastInfo i in FDforecastList)
                        {
                            if (i.ID == o.ID) count = i.Count;
                        }
                        ViewPurlistView.Items.Add(new ListViewItem(new string[] {"配菜", s, o.ObjectName, count.ToString(), "0" }));
                        ForecastID1[IDsum1] = o.ID;
                        IDsum1++;
                    }
                }

           // }
        }

        private void ViewPurlistView_DoubleClick(object sender, EventArgs e)
        {
            int slvi = ViewPurlistView.SelectedIndices[0];
            ListViewItem lvi = ViewPurlistView.SelectedItems[0];
            TextBox tb = new TextBox();
            tb.Width = ViewPurlistView.Items[slvi].SubItems[4].Bounds.Width;
            tb.LostFocus += new EventHandler(this.Text_Blur2);
            tb.Location = new Point(ViewPurlistView.Items[slvi].SubItems[4].Bounds.Left, lvi.Bounds.Top);
            ViewPurlistView.Controls.Add(tb);
            tb.Focus();
            tb.Text = ViewPurlistView.Items[slvi].SubItems[4].Text;
        }

        private void Text_Blur2(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            ListViewItem lvi = ViewPurlistView.SelectedItems[0];
            int slvi = ViewPurlistView.SelectedIndices[0];
            ViewPurlistView.Items[slvi].SubItems[4].Text = tb.Text;
            tb.Parent.Controls.Remove(tb);
        }
        //生成采购单

        private void button1_Click(object sender, EventArgs e)
        {
            IDsum = 0;
            IDsum1 = 0;
            for (int k = 0; k < Nums.Length; k++)
            {
                Nums[k] = Convert.ToInt32(ViewPurlistView.Items[k].SubItems[4].Text);
                IDsum++;
            }


            for (int k = IDsum; k < (Nums1.Length + IDsum); k++)
            {
                Nums1[k-IDsum] = Convert.ToInt32(ViewPurlistView.Items[k].SubItems[4].Text);
            }



            List<MaterialObject> matList = op.LoadMaterialObjects();
            List<MaterialObject> tmpList = op.LoadMaterialObjects();
            for (int i = 0; i < tmpList.Count(); i++)
            {
                tmpList[i].Stock = 0;
            }

            //计算单品所需要的原料
            for (int i = 0; i < ForecastID.Length; i++)
            {
                List<MaterialParent> matpList = op.LoadMaterialParent(ForecastID[i], 0);
                MaterialObject mo = new MaterialObject();

                foreach (MaterialParent m in matpList)
                {
                    for (int k = 0; k < tmpList.Count(); k++)
                    {
                        if (m.ChildID == tmpList[k].ID)
                        {
                            tmpList[k].Stock += (float)m.Dosage * Nums[i];
                        }
                    }
                }
            }
            //计算配菜所需要的原料
            for (int i = 0; i < ForecastID1.Length; i++)
            {
                List<MaterialParent> matpList = op.LoadMaterialParent(ForecastID1[i], 1);
                MaterialObject mo = new MaterialObject();

                foreach (MaterialParent m in matpList)
                {
                    for (int k = 0; k < tmpList.Count(); k++)
                    {
                        if (m.ChildID == tmpList[k].ID)
                        {
                            tmpList[k].Stock += (float)m.Dosage * Nums1[i];
                        }
                    }
                }
            }


            PurchaseTableForm ptf = new PurchaseTableForm(matList, tmpList);
            ptf.ShowDialog();
        }
    }
}
